import org.kwis.msp.lcdui.*;
import java.lang.*;


public class GameManager implements Runnable{

	private ScreenManager screen;

	private GameMemberGroup aliens;
	private GameMember player;
	private GameMemberGroup playerGroup;
	private GameMemberGroup missilesPlayer;
	private GameMember ufo;
	private GameMemberGroup missilesUFO;	

	private boolean indicatorStop = true;
	private int alienDelay;

	public GameManager(ScreenManager screen){
		this.screen= screen;
		screen.setGameManager(this);
		initializeGame();
	}

//------------- initializeGame() --------------------------
	void initializeGame(){
		Image[] image;
		int i,j;

		aliens = new GameMemberGroup();
		//3Step에서 추가(시작)
		aliens.setStepHorizontal(2);
		aliens.setStepVertical(5);
		aliens.setBoundaryLeft(10);
		aliens.setBoundaryRight(screen.getWidth());
		aliens.setBoundaryBottom(screen.getHeight());
		//3Step에서 추가 (끝)


		// Alien  이미지 그리기
		for(i=0;i<4;i++){
			for(j=0; j<5; j++){ 
				
				if(i==0||i==2){
					image = new Image[] {GameMemberPainter.createImage("ALIEN1_1"),
										 GameMemberPainter.createImage("ALIEN1_2")};
					aliens.addElement(new GameMember(15 * j , 15*i+15, image));
				}

				if(i==1){
					image = new Image[] {GameMemberPainter.createImage("ALIEN2_1"),
										 GameMemberPainter.createImage("ALIEN2_2")};
					aliens.addElement(new GameMember(15 * j , 15*i+15, image));
				}

				if(i==3){
					image = new Image[] {GameMemberPainter.createImage("ALIEN3_1"),
										 GameMemberPainter.createImage("ALIEN3_2")};
					aliens.addElement(new GameMember(15 * j , 15*i+15, image));
				}

			}
		}//for

		image = new Image[] {GameMemberPainter.createImage("PLAYER")};
		player = new GameMember(screen.getWidth()/2,screen.getHeight()-image[0].getHeight(),image);

		playerGroup = new GameMemberGroup();
		playerGroup.addElement(player);

	
	} // initializeGame()

//---------------- start() ----------------------------------


	public void start(){
		Thread runner = new Thread(this);
		runner.start();
	}


//--------------- run() --------------------------

	public void run(){

//3Step에서 추가 (시작)
		int stepper = 0;

		indicatorStop=false;

		while(!indicatorStop){
			synchronized(this){
				try{
					Thread.sleep(5);
				}catch(Exception e){}

				alienDelay = aliens.size()/5 +1;

				if(stepper++ % alienDelay == 0)
					aliens.moveHorizontal();

			} //synchronized
		
				screen.repaint();
			
		}//while
//3Step에서 추가 (끝)
	
	}

//----------------- getAliens() --------------------


	 public GameMemberGroup getAliens() {
    
        return aliens;
    }

//------------- getPlayer() -----------------------


	 public GameMember getPlayer() {
    
        return player;
    }

//-------------- getPlayerGroup() ----------------------

	 public GameMemberGroup getPlayerGroup() {
    
        return playerGroup;
    }

} //GameManager
